package us.nchu.tomtiger.controller;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import us.nchu.tomtiger.service.SmsVerifyCodeService;
import us.nchu.tomtiger.model.Result;
import us.nchu.tomtiger.model.SmsVerifyCode;

import javax.annotation.Resource;

/**
 * Created by XiongXuan on 2017/12/31.
 */

@RestController
@RequestMapping("/SmsVerifyCode")
public class MobileValidationController {
    @Resource
    private SmsVerifyCodeService smsVerifyCodeService;

    @PostMapping
    public ResponseEntity<Result> sendSmsVerifyCode(String phoneNumber) {
        SmsVerifyCode smsVerifyCode = new SmsVerifyCode();
        smsVerifyCode.setPhoneNumber(phoneNumber);

        // create random code
        smsVerifyCode.setCode(randomCode());

        // 发送短信
        sentVerifiCodeSMS(smsVerifyCode.getPhoneNumber(), smsVerifyCode.getCode());

        // response body
        Result result = new Result();

        // 写入数据库
        boolean status = smsVerifyCodeService.insert(smsVerifyCode);
        if (!status) {
            result.setCode(0);
            result.setMsg("failed");
        }
        else {
            result.setCode(1);
            result.setMsg("success");
        }

        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    /**
     * 生成4位随机数
     * @return
     */
    private String randomCode() {
        int a = (int) (Math.random() * 10000);
        String code = String.format("%04d", a);
        return code;
    }

    /**
     * 发送短信验证码
     * @param phoneNumber
     * @param verifiCode
     */
    private void sentVerifiCodeSMS(String phoneNumber, String verifiCode) {
        TaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest", "24748606",
                "a2707672744fee8b2a7d5eb4bd314ae5");
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();

        req.setSmsType( "normal" );
        req.setSmsFreeSignName( "创智行" );
        req.setSmsParamString("{code:'" + verifiCode + "'}");
        req.setRecNum(phoneNumber);
        req.setSmsTemplateCode("SMS_118610132");
        AlibabaAliqinFcSmsNumSendResponse rsp;
        try {
            rsp = client.execute(req);
            System.out.println(rsp.getBody());
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }
}
